package net.nshc.kwkim.mycalc.activity;

import net.nshc.kwkim.mycalc.*;
import net.nshc.kwkim.mycalc.db.*;
import net.nshc.kwkim.mycalc.db.DataBases.CreateDB;
import android.app.*;
import android.content.*;
import android.database.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class HistoryList extends Activity {
	private DBOpenHelper openHelper;
	private SimpleCursorAdapter mAdapter;
	private ListView list;
	private TextView empty;
	public static Context mContext;
	private Cursor c;
	private int mPosition;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historylist);

		list = (ListView)findViewById(R.id.list);
		empty = (TextView)findViewById(R.id.empty);
		list.setOnItemClickListener(mItemClickListener);
		registerForContextMenu(list);
		openHelper = new DBOpenHelper(this);
		openHelper.open();
		mContext=this;
	}

	@Override 
	protected void onResume(){
		super.onResume();
		openHelper = new DBOpenHelper(this);
		fetchData();
	}

	private void fetchData() {
		c = openHelper.getAllColumns();
		
		if(c.getCount() == 0){
			list.setVisibility(View.GONE);
			empty.setVisibility(View.VISIBLE);
		}else{
			list.setVisibility(View.VISIBLE);
			empty.setVisibility(View.GONE);
		}

		startManagingCursor(c);

		String[] FROM = new String[]{CreateDB.ID, CreateDB.RESULT, CreateDB.LOC, CreateDB.TIME};
		int[] TO = new int[]{R.id.num, R.id.hisret,R.id.hisloc,R.id.hisdate};
		
		if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_HISTORYACTIVITY, "From length = "+FROM.length);
		
		mAdapter = new SimpleCursorAdapter(this, R.layout.historyrow,c,FROM,TO);
		list.setAdapter(mAdapter);
	}

	AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			mPosition = position;
			c.moveToPosition(mPosition);
			new AlertDialog.Builder(HistoryList.this)
			.setTitle("삭제")
			.setMessage("결과 : "+c.getString(1)+"\n위치 : "+c.getString(2)+"\n\n히스토리를 지우시겠습니까?")
			.setPositiveButton("예", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(MyCalcActivity.bLog)Log.v(LogDefine.LOG_HISTORYACTIVITY, "OptionMenu_Debug_It is 삭제");
					c.moveToPosition(mPosition);
					if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_HISTORYACTIVITY, "ItemSelected"+c.getString(0));
					openHelper.deleteColumn(c.getInt(0));
					fetchData();
				}
			})
			.setNegativeButton("취소", null)
			.show();
		}
	};

	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuItem item; 
		item = menu.add(0, 1, 0, "전체 삭제");
		item.setAlphabeticShortcut('a');

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case 1 :
			new AlertDialog.Builder(this)
			.setTitle("전체삭제")
			.setMessage("히스토리를 전부 지우시겠습니까?")
			.setPositiveButton("예", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(MyCalcActivity.bLog)Log.v(LogDefine.LOG_HISTORYACTIVITY, "OptionMenu_Debug_It is 전체 삭제");
					openHelper.deleteAllColumn();
					fetchData();
				}
			})
			.setNegativeButton("취소", null)
			.show();
			return true;
		}
		return false;	
	}

}
